<?php

namespace App\Http\Controllers;

use Laravel\Lumen\Routing\Controller as BaseController;

class Controller extends BaseController
{
    /**
     * 成功返回json
     */
    public function success($data = [], $msg = 'success', $code = 200)
    {
        return ['code' => $code, 'msg' => $msg, 'data' => $data];
    }

    /**
     * 失败返回json
     */
    public function fail($msg = 'fail' ,$data = [],  $code = 500){
        return ['code' => $code, 'msg' => $msg, 'data' => $data];
    }

    /**
     * 请求输入验证
     * @param $data
     * @param $rules
     * @param $messages
     * @return array|bool
     */
    public function verify($data, $rules, $messages)
    {
        $validate = app('validator')->make($data, $rules, $messages);
        if($validate->fails()){
            return $this->fail('fail', $validate->errors()->all()); //验证失败, 返回验证信息
        }
        return false;   //验证成功, 返回false
    }
}
